#include "Test.h"
#include <stdio.h>

CTestFactory TestFactory;

CTest::CTest()
{
}

CTest::~CTest()
{
}

ULONG __stdcall CTest::XComTest::AddRef()
{
    return 1;
}

ULONG __stdcall CTest::XComTest::Release()
{
    return 0;
}

HRESULT __stdcall CTest::XComTest::QueryInterface(REFIID iid, void** ppvObj)
{
    if (!ppvObj)
        return E_POINTER;

    *ppvObj = nullptr;

    if (iid == IID_IUnknown)
    {
        *ppvObj = this;
    }
    else if (iid == IID_ITest)
    {
        *ppvObj = this;
    }

    if (*ppvObj)
    {
        ((IUnknown*)*ppvObj)->AddRef();
        return S_OK;
    }

    return E_NOINTERFACE;
}

HRESULT __stdcall CTest::XComTest::Test()
{
    printf("COM Thread Id: 0x%X\r\n", GetCurrentThreadId());
    MessageBox(nullptr, nullptr, nullptr, 0);
    return S_OK;
}

CTestFactory::CTestFactory()
{
}

CTestFactory::~CTestFactory()
{
}

HRESULT __stdcall CTestFactory::CreateInstance(
    IN OPTIONAL IUnknown* pUnkOuter, 
    IN REFIID riid, 
    OUT void** ppvObject
)
{
    CTest* lpTest = new CTest;
    return lpTest->m_xComTest.QueryInterface(riid, ppvObject);
}

HRESULT __stdcall CTestFactory::LockServer(IN BOOL fLock)
{
    return S_OK;
}
